package com.dopeydev.testcar;

import java.util.ArrayList;
import java.util.List;

import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dopeydev.broadcast.FragmentManagerBroadcast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapViewFragment extends MapFragment implements
		OnMarkerDragListener, OnMarkerClickListener {

	public final static String TAG = MapViewFragment.class.getCanonicalName();
	GoogleMap map;

	public static MapViewFragment getInstance() {
		MapViewFragment frag = new MapViewFragment();

		return frag;
	}

	private FragmentManagerBroadcast mFragmentManagerBroadcast = new FragmentManagerBroadcast() {

		@Override
		public void locationAcquired() {

			if (getFragmentManager().findFragmentById(R.id.containerOverlay) != null) {
				if (getFragmentManager().findFragmentByTag(
						ButtonTestFragment.TAG) != null) {
					getFragmentManager()
							.beginTransaction()
							.remove(getFragmentManager().findFragmentByTag(
									ButtonTestFragment.TAG)).commit();
				}
			}
		}

		@Override
		public void zoomToLocation(LatLng currentLocation) {
			zoomToCurrentLocation(currentLocation);
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		map = getMap();
		UiSettings settingsUI = map.getUiSettings();
		settingsUI.setZoomControlsEnabled(false);
		settingsUI.setCompassEnabled(false);
		map.setTrafficEnabled(false);
		map.setMyLocationEnabled(true);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		map.setOnMarkerDragListener(this);
		map.setOnMarkerClickListener(this);

		List<LatLng> iterableLatLng = new ArrayList<LatLng>();

		LatLng zero = new LatLng(41.8715599, 12.5761051);
		iterableLatLng.add(zero);
		LatLng first = new LatLng(41.8712467, 12.5783639);
		iterableLatLng.add(first);
		LatLng second = new LatLng(41.8715331, 12.5785381);
		iterableLatLng.add(second);
		LatLng third = new LatLng(41.8715331, 12.5785381);
		iterableLatLng.add(third);
		LatLng fourth = new LatLng(41.8684506, 12.5872216);
		iterableLatLng.add(fourth);
		LatLng fifth = new LatLng(41.8602588, 12.5856155);
		iterableLatLng.add(fifth);
		LatLng sixth = new LatLng(41.8587191, 12.5913845);
		iterableLatLng.add(sixth);

		PolylineOptions polyOpt = new PolylineOptions().width(20).zIndex(10)
				.color(Color.parseColor("#33ccff")).addAll(iterableLatLng);
		map.addPolyline(polyOpt);
		// setupMap();

	}

	private void zoomToCurrentLocation(LatLng currentLocation) {

		map.animateCamera(CameraUpdateFactory
				.newLatLngZoom(currentLocation, 15));

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onStart() {
		IntentFilter filterFrag = new IntentFilter();
		filterFrag.addAction(FragmentManagerBroadcast.Action.LOCATION_ACQUIRED);
		LocalBroadcastManager.getInstance(getView().getContext())
				.registerReceiver(mFragmentManagerBroadcast, filterFrag);
		super.onStart();
	}

	@Override
	public void onStop() {
		LocalBroadcastManager.getInstance(getView().getContext())
				.unregisterReceiver(mFragmentManagerBroadcast);
		super.onStop();
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onMarkerDrag(Marker marker) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMarkerDragEnd(Marker marker) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMarkerDragStart(Marker marker) {
		// TODO Auto-generated method stub

	}

}
